package com.yoke.common.base;

import com.yoke.util.FileUtil;
import com.yoke.util.StringUtil;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created by jiangzeyin on 2017/6/30.
 */
public abstract class DiggingBaseControl extends NginxBaseControl {

//    protected void convertUrl(Elements elements, String url, String attrName) {
//        elements.forEach(element -> {
//            String src = element.attr(attrName);
//            if (StringUtil.IsEmpty(src))
//                return;
//            element.attr(attrName, convertUrl(src, url));
//        });
//    }

    protected String convertUrl(String src, String url) {
        if (src.startsWith("http"))
            return src;
        if (src.startsWith("//")) {
            src = (url.startsWith("http://") ? "http:" : "https:") + src;
            return src;
        }
        if (src.startsWith("/")) {
            String tempUrl = url;
            while (true) {
                int eIndex = tempUrl.lastIndexOf("/");
                int sIndex = tempUrl.indexOf("/");
                if (sIndex + 1 == eIndex)
                    break;
                tempUrl = tempUrl.substring(0, eIndex);
            }
            return FileUtil.ClearPath(tempUrl + "/" + src);
        } else {
            int end = url.lastIndexOf("/");
            int start = url.indexOf("/");
            String tempUrl;
            if (start + 1 < end) {
                tempUrl = url.substring(0, end);
            } else {
                tempUrl = url + "/";
            }
            url = url.replace("://", "");
            end = url.lastIndexOf("/");
            start = url.indexOf("/");
            if (end == start) {
                if (src.startsWith("../")) {
                    src = src.substring(2);
                } else if (src.startsWith("./")) {
                    src = src.substring(1);
                }
            } else {
                if (src.startsWith("./")) {
                    src = src.substring(1);
                } else {
                    while (true) {
                        if (src.startsWith("../")) {
                            src = src.substring(2);
                            if (tempUrl.endsWith("/"))
                                tempUrl = tempUrl.substring(0, tempUrl.length() - 1);
                            int end_index = tempUrl.lastIndexOf("/");
                            tempUrl = tempUrl.substring(0, end_index);
                        } else {
                            break;
                        }
                    }
                }
            }
            return FileUtil.ClearPath(tempUrl + "/" + src);
        }
    }

    /**
     * 备份文件
     *
     * @param file
     */
    public static void backIndexHtml(File file) {
        int count = 5;
        while (count-- >= 0) {
            File in = count == 0 ? new File(file, "index.html") : new File(file, String.format("index_%s.html", count));
            if (!in.exists())
                continue;
            File out = new File(file, String.format("index_%s.html", count + 1));
            try {
                FileCopyUtils.copy(in, out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取记录列表
     *
     * @param file
     * @return
     */
    public static File[] listLogs(File file) {
        File[] files = file.listFiles((dir, name) -> {
            String[] temps = StringUtil.StringToArray(name, "_");
            if (temps == null)
                return false;
            if (temps.length == 2) {
                if (!"index".equals(temps[0]))
                    return false;
                temps = StringUtil.StringToArray(temps[1], ".");// temps[1].split(".");
                if (temps.length != 2)
                    return false;
                int index = StringUtil.parseInt(temps[0]);
                if (index >= 1 && index <= 5)
                    return true;
            }
            return false;
        });
        assert files != null;
        Arrays.sort(files, (o1, o2) -> {
            long t1 = o1.lastModified();
            long t2 = o2.lastModified();
            if (t1 > t2)
                return -1;
            return 1;
        });
        return files;
    }
}
